﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace XiAnAirport.BUS
{
    public class LogHelper
    {
        static string path = AppDomain.CurrentDomain.BaseDirectory;
      public  static int logflag = 0;
        public static void Log(string message)
        {
            try
            {
                string dirpath = path + @"/Log";
                string filepath = dirpath + "/Log_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";


                //生成目录
                //创建文件夹
                if (Directory.Exists(dirpath) == false)//如果不存在就创建file文件夹
                {
                    Directory.CreateDirectory(dirpath);
                }

                // 判断文件是否存在，不存在则创建，否则读取值显示到txt文档
                if (!System.IO.File.Exists(filepath))
                {
                    FileStream fs1 = new FileStream(filepath, FileMode.Create, FileAccess.Write);//创建写入文件 
                    fs1.Close();
                }

                File.AppendAllTextAsync(filepath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + ":" + message + "\r\n");
            }
            catch
            {
                return;
            }
        }

        public static void Print(string message, string elevatorname)
        {
            string dirpath = path + @"/Log/Elevator";
            string filepath = dirpath + "/Elevator_" + elevatorname + ".txt";

            //生成目录
            //创建文件夹
            if (Directory.Exists(dirpath) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(dirpath);
            }

            // 判断文件是否存在，不存在则创建，否则读取值显示到txt文档
            if (!System.IO.File.Exists(filepath))
            {
                FileStream fs1 = new FileStream(filepath, FileMode.Create, FileAccess.Write);//创建写入文件 
                fs1.Close();
            }

            FileStream stream = File.Open(filepath, FileMode.OpenOrCreate, FileAccess.Write);
            stream.Seek(0, SeekOrigin.Begin);
            stream.SetLength(0);
            stream.Close();

            File.AppendAllTextAsync(filepath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + message + "\r\n");
        }

        public static void MqttLog(string message)
        {
            try
            {
                if (logflag==1)
                {
                    string dirpath = path + @"/MqttLog";
                    string filepath = dirpath + "/Log_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

                    //生成目录
                    //创建文件夹
                    if (Directory.Exists(dirpath) == false)//如果不存在就创建file文件夹
                    {
                        Directory.CreateDirectory(dirpath);
                    }

                    // 判断文件是否存在，不存在则创建，否则读取值显示到txt文档
                    if (!System.IO.File.Exists(filepath))
                    {
                        FileStream fs1 = new FileStream(filepath, FileMode.Create, FileAccess.Write);//创建写入文件 
                        fs1.Close();
                    }

                    File.AppendAllTextAsync(filepath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + ":" + message + "\r\n");
                }
            }
            catch
            {
                return;
            }
        }
    }
}
